
function validateEmail(email) {
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    return emailPattern.test(email);
} 
function tggv_form_alert(input_id, msg){
    var height_input    =   $('#' + input_id).height();
    var width_input     =   $('#' + input_id).width();
    var offset          =   $('#' + input_id).offset();
    
    var html	=	'<div id="form_alert_layer">' + msg + '</div>';
    $('body').append(html);
    $('#form_alert_layer').css({
        'height': 20,
	'display': 'block',
        'font-size': '12px',
        'padding-top': '5px',
        'padding-left': '5px',
        'padding-right': '5px',
        'text-align': 'center',
        'color': '#fff',
        'font-weight': 'bold',
        'background-color': '#2BA104',
	'left': offset.left,
        'position': 'absolute',
	'top': offset.top - height_input - 5
    });
    $('#form_alert_layer').click(function(){
        tggv_hide_form_alert();
    });
    setTimeout(function(){
        tggv_hide_form_alert();
    }, 4000);
}
function tggv_hide_form_alert(){
    if($('#form_alert_layer').attr('id')){
        $('#form_alert_layer').fadeOut(800, function(){
            $('#form_alert_layer').remove();
        });
    }
}
/** email newsletter */
function submit_newsletter(){
    var email   =   $('#email-news-letter').val();
    if(!validateEmail(email)){
        tggv_form_alert('email-news-letter', 'Vui lòng nhập 1 email hợp lệ');
        $('#email-news-letter').focus();
        return;
    }
    
    $.ajax({
          type: 'POST',
          data: {email: email},
          dataType: 'html',
          url: baseUrl + '/newsletters/add',
          success: function(data) {
             alert('bạn đã đăng ký nhận email thành công');
             $('#email-news-letter').val('');
          }
   });
}
/** email newsletter */

/* search google */
function submit_search(){
    if(!$('#quick-search-input').attr('id')){
        return;
    }
    if($('#quick-search-input').val() == ''){
        tggv_form_alert('quick-search-input', 'Vui lòng nhập 1 từ khóa');
        return;
    }
    $('#searchform').submit();
}

/** poll */
function vote_poll(){
    var option_selected =   $(".poll-row input:checked");
    if(!$(option_selected).attr('id')){
        alert('Vui lòng chọn 1 lựa chọn!');
        return;
    }
    $.ajax({
          type: 'POST',
          data: {optionid: $(option_selected).val()},
          dataType: 'html',
          url: baseUrl + '/polls/vote',
          success: function(data) {
             if(data == '1'){
                alert('Bạn đã bình chọn thành công');
             }else{
                 alert('Bạn đã bình chọn rồi!');
             }
          }
    });
}


function setHomepage()
{
    if (document.all)
    {
        document.body.style.behavior = 'url(#default#homepage)';
        document.body.setHomePage(baseUrl);
    }
    else if (window.sidebar)
    {
        if (window.netscape)
        {
            try
            {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch(e)
            {
                return;
            }
        }
        var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
        prefs.setCharPref('browser.startup.homepage', baseUrl);
    }
}
/* set bookmark */
function setBookmark(){
	if(document.all){
		window.external.AddFavorite(location.href,document.title);
	}else if(window.sidebar){
		window.sidebar.addPanel (document.title,location.href,'');
	}
	else if(window.chrome){
    	alert('dùng phím tắt ctrl+D để bookmark (Command+D cho macs) sau đó click Ok');
    }
}
/* set bookmark */

/* slideshow new game */
var lastestgame_record_index =   0;
var int_lastestgame     =   null;

function auto_slideshow_lastestgame(){
    var list_lastestgame = $.makeArray($('.list-lastest-news'));
    if(list_lastestgame.length <= 1){
        return;
    }
    if(lastestgame_record_index > list_lastestgame.length - 1){
        lastestgame_record_index = 0;
    }
    $('.list-lastest-news').hide();
    $(list_lastestgame[lastestgame_record_index]).fadeIn(800);
    lastestgame_record_index++;
    int_lastestgame = setTimeout(function(){
        auto_slideshow_lastestgame();
    },5000);
}
/*slide show new game */



/* slideshow hot game */
var hotgame_record_index =   0;
var int_hotgame     =   null;
function auto_slideshow_hotgame(){
    var list_hotgame = $.makeArray($('.top-game .list-game'));
    if(list_hotgame.length <= 1){
        return;
    }
    if(hotgame_record_index > list_hotgame.length - 1){
        hotgame_record_index = 0;
    }
    $('.top-game .list-game').hide();
    $(list_hotgame[hotgame_record_index]).fadeIn(800);
    hotgame_record_index++;
    int_hotgame = setTimeout(function(){
        auto_slideshow_hotgame();
    },15000);
}
function slideshow_hotgame_next(){
    clearTimeout(int_hotgame);
    var list_hotgame = $.makeArray($('.top-game .list-game'));
    if(hotgame_record_index >= list_hotgame.length){
        hotgame_record_index = 0;
    }
    auto_slideshow_hotgame();
}
function slideshow_hotgame_pre(){
    hotgame_record_index--;
	hotgame_record_index--;
    clearTimeout(int_hotgame);
    var list_hotgame = $.makeArray($('.top-game .list-game'));
    if(hotgame_record_index < 0){
        hotgame_record_index = 0;
    }
    auto_slideshow_hotgame();
}
/* end slideshow hot game */

/* load game hay nhat cho 1 danh muc khi click vao tab */
function loadHotgamebycategoryAjax(catid, current_li){
	var tab_class           =	'tab-block-' + catid;
	var gamelist_class	=	'home-block-' + catid;
	var layer_idname	=	'layerid-' + catid;

        show_screen_loader_ajax(gamelist_class, layer_idname);
        
	var li_tabs = $('.' + tab_class).find('li');
	$(li_tabs).each(function(index, el){
		if($(el).hasClass('active')){
			$(el).removeClass('active');
		}
	});
	$(current_li).addClass('active');
        $.ajax({
          type: 'GET',
          dataType: 'html',
          url: baseUrl + '/games/service_get_hotgameincat/catid:' + catid,
          success: function(data) {
             if(data != '0'){
                $('.' + gamelist_class).html(data);

             }
          }
        });
        close_screen_loader_ajax(layer_idname);
}
/* load game moi nhat cho 1 danh muc khi click vao tab */
function loadLastestgamebycategoryAjax(catid, current_li){
	var tab_class           =	'tab-block-' + catid;
	var gamelist_class	=	'home-block-' + catid;
	var layer_idname	=	'layerid-' + catid;

        show_screen_loader_ajax(gamelist_class, layer_idname);
        
	var li_tabs = $('.' + tab_class).find('li');
	$(li_tabs).each(function(index, el){
		if($(el).hasClass('active')){
			$(el).removeClass('active');
		}
	});
	$(current_li).addClass('active');
        $.ajax({
          type: 'GET',
          dataType: 'html',
          url: baseUrl + '/games/service_get_lastestgameincat/catid:' + catid,
          success: function(data) {
             if(data != '0'){
                $('.' + gamelist_class).html(data);

             }
          }
        });
        close_screen_loader_ajax(layer_idname);
        
}
/* load game nhieu nguoi choi nhat cho 1 danh muc khi click vao tab */
function loadPopulargamebycategoryAjax(catid, current_li){
	var tab_class           =	'tab-block-' + catid;
	var gamelist_class	=	'home-block-' + catid;
	var layer_idname	=	'layerid-' + catid;

	show_screen_loader_ajax(gamelist_class, layer_idname);

	var li_tabs = $('.' + tab_class).find('li');
	$(li_tabs).each(function(index, el){
		if($(el).hasClass('active')){
			$(el).removeClass('active');
		}
	});
	$(current_li).addClass('active');

        $.ajax({
          type: 'GET',
          dataType: 'html',
          url: baseUrl + '/games/service_get_populargameincat/catid:' + catid,
          success: function(data) {
             if(data != '0'){
                $('.' + gamelist_class).html(data);
             }
          }
        });
        close_screen_loader_ajax(layer_idname);
}

function show_screen_loader_ajax(classname_blockgame, id){
	var width	=	$('.' + classname_blockgame).width();
	var height	=	$('.' + classname_blockgame).height();
	var offset	=	$('.' + classname_blockgame).offset();

	var html	=	'<div class="ajax_loader_layer" id="'+ id + '"><img src="/img/input-loading.gif" /></div>';
	$('body').append(html);
	$('#' + id).find('img').css({
		'left': (width/2) - 12,
		'top': (height/2) - 12
	});
	$('#' + id).css({
		'width' : width,
		'height' : height,
		'display': 'block',
		'left': offset.left,
                'position': 'absolute',
		'top': offset.top
	});
}
function close_screen_loader_ajax(id){
	$('#' + id).remove();
}

window.onload = function(){
	$(document).ready(function(){
            if(screen.width < 1280){
                $('body').width(1280);
            }else{
                $('body').width(screen.width);
            }
            if($('.top-game .list-game').attr('class')){
                auto_slideshow_hotgame();
            }
            if($('.list-lastest-news').attr('class')){
                auto_slideshow_lastestgame();
            }
	});
};